package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.api.entity.Xsjb;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;

import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.rpc.ParameterMode;

import java.io.StringReader;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by xuhb on 2016/4/12.
 */
@Controller
@RequestMapping("weixin/ems")
public class EMSQueryController extends BaseController {

    //private String emsUri = "http://123.56.191.63:8080/EMS/services/EmsService?wsdl";
    private String emsUri = "http://123.56.191.63:8080/EMS/services/EmsService";
    private final String emsNamespace = "http://service.api.ems.com";

    /**
     * EMS查询首页
     */
    @RequestMapping(value = "emsIndex", method = GET)
    public String gotoIndex(HttpServletRequest request, Model model) throws Exception {
        return "weixin/bmsrv/emscx";
    }

    @RequestMapping(value = "queryEms", produces = {"application/text;charset=UTF-8"})
    @ResponseBody
    public String emsQuery(HttpServletRequest request, Model model) throws Exception {
        // 用户信息
        String userInfo = "<UserSetting><username>gatjlweb</username><userkey>D2AC3C8EA1FF0D97E46F580C246F21DD</userkey><userdw>大庆省公安厅门户</userdw></UserSetting>";
        // 运单号
        String mailNo = request.getParameter("mailNo"); //"9890012945289";
        String result = "";
        String returnValue = "";
        try {
            Service service = new Service();

            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(emsUri);
            // 接口提供的方法名
            call.setOperationName("QueryMail");
            // 接口的参数
            call.addParameter("in0", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("in1", XMLType.XSD_STRING, ParameterMode.IN);

            // 设置返回类型
            call.setReturnType(XMLType.XSD_STRING);

            Object[] param = new Object[]{mailNo, userInfo};
            result = (String) call.invoke(param);
            // 解析webservice返回的xml
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(new InputSource(new StringReader(result)));
            // 节点的长度
            int elementLength = document.getElementsByTagName("trace").getLength();
            for (int i = 0; i < elementLength; i++) {
                // 发生时间
                String time = document.getElementsByTagName("acceptTime").item(i).getTextContent();
                //String acceptAddress = document.getElementsByTagName("acceptAddress").item(i).getTextContent();
                // 备注
                String remark = document.getElementsByTagName("remark").item(i).getTextContent();
                returnValue = time + " " + remark + "<br/>" + returnValue;
            }

            if ("".equals(returnValue)) {
                returnValue = "暂无记录！";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }


    /**
     * Ems查询
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "queryYtEms", produces = {"application/text;charset=UTF-8"})
    @ResponseBody
    public String emsYtQuery(HttpServletRequest request, Model model) throws Exception {
        String returnValue = "";
        try {
            String url = configInfo.getServiceInterfaceAddress()+"/mail/ems/ems-trackmail/" + request.getParameter("mailNo") + "/wx?genGuid=osc_wx&validCode=osc_wx_valid";
            JSONObject object = JSON.parseObject(HttpClientUtil.doGet(url));
            for (int i = 0; i < object.getJSONObject("data").getJSONArray("traces").size(); i++) {
                // 发生时间
                String time = object.getJSONObject("data").getJSONArray("traces").getJSONObject(i).getString("acceptTime");
                // 备注
                String remark = object.getJSONObject("data").getJSONArray("traces").getJSONObject(i).getString("remark");
                returnValue = time + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + remark + "<br/>" + returnValue;
            }
            if ("".equals(returnValue)) {
                returnValue = "暂无记录！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnValue;
    }

}
